package com.mazaiting.instructions.constants;

import com.mazaiting.instructions.base.NoOperandsInstruction;
import com.mazaiting.rtda.MtFrame;

/**
 * 把隐含在操作码中的常量值推入操作数栈顶
 *
 * @author mazaiting
 * @since 2021/8/14
 */
public class ConstInstruction {
    /**
     * 常量 NULL
     */
    public static class AConstNull extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushRef(null);
        }
    }

    /**
     * 常量 双精度 0.0D
     */
    public static class DConst0 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushDouble(0.0D);
        }
    }

    /**
     * 常量 双精度 1.0D
     */
    public static class DConst1 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushDouble(1.0D);
        }
    }

    /**
     * 常量 单精度 0.0F
     */
    public static class FConst0 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushFloat(0.0F);
        }
    }

    /**
     * 常量 单精度 1.0F
     */
    public static class FConst1 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushFloat(1.0F);
        }
    }

    /**
     * 常量 单精度 2.0F
     */
    public static class FConst2 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushFloat(2.0F);
        }
    }

    /**
     * 常量 整型 -1
     */
    public static class IConstM1 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(-1);
        }
    }

    /**
     * 常量 整型 0
     */
    public static class IConst0 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(0);
        }

        @Override
        public String toString() {
            return "iconst_0";
        }
    }

    /**
     * 常量 整型 1
     */
    public static class IConst1 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(1);
        }

        @Override
        public String toString() {
            return "iconst_1";
        }
    }

    /**
     * 常量 整型 2
     */
    public static class IConst2 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(2);
        }

        @Override
        public String toString() {
            return "iconst_2";
        }
    }

    /**
     * 常量 整型 3
     */
    public static class IConst3 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(3);
        }

        @Override
        public String toString() {
            return "iconst_3";
        }
    }

    /**
     * 常量 整型 4
     */
    public static class IConst4 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(4);
        }

        @Override
        public String toString() {
            return "iconst_4";
        }
    }

    /**
     * 常量 整型 5
     */
    public static class IConst5 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushInt(5);
        }
    }

    /**
     * 常量 长整型 0
     */
    public static class LConst0 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushLong(0L);
        }
    }

    /**
     * 常量 长整型 1
     */
    public static class LConst1 extends NoOperandsInstruction {
        @Override
        public void execute(MtFrame frame) {
            frame.getOperandStack().pushLong(1L);
        }
    }
}
